package guowenlong.golf.ui.friend.myfriends;


import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import cc.solart.turbo.BaseTurboAdapter;
import cc.solart.turbo.BaseViewHolder;
import guowenlong.golf.R;
import java.util.List;

public class AddContansAdapter extends BaseTurboAdapter<ContactsInfo, BaseViewHolder> {

  public AddContansAdapter(Context context) {
    super(context);
  }

  public AddContansAdapter(Context context, List<ContactsInfo> data) {
    super(context, data);
  }

  @Override
  protected int getDefItemViewType(int position) {
    ContactsInfo contactsInfo = getItem(position);
    return contactsInfo.getType();
  }

  @Override
  protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
    if (viewType == 0) {
      return new ContactsHolder(inflateItemView(R.layout.item_contacts, parent));
    } else {
      return new PinnedHolder(inflateItemView(R.layout.item_pinned_header, parent));
    }
  }

  @Override
  protected void convert(BaseViewHolder holder, ContactsInfo item) {
    if (holder instanceof ContactsHolder) {
      ((ContactsHolder) holder).contacts_name.setText(item.getName());
    } else {
      String letter = item.getName().substring(0, 1);
      ((PinnedHolder) holder).city_tip.setText(letter);
    }
  }

  public int getLetterPosition(String letter) {
    for (int i = 0; i < getData().size(); i++) {
      if (getData().get(i).getType() == 1 && getData().get(i).getName().equals(letter)) {
        return i;
      }
    }
    return -1;
  }

  class ContactsHolder extends BaseViewHolder {

    TextView contacts_name;
    TextView tvAddContacts;

    public ContactsHolder(View view) {
      super(view);
      contacts_name = findViewById(R.id.city_name);
      tvAddContacts = findViewById(R.id.tv_add_contacts);
      tvAddContacts.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
          String text = tvAddContacts.getText().toString();
          switch (text){
            case "添加":
              tvAddContacts.setText("已添加");
              tvAddContacts.setTextColor(v.getResources().getColor(R.color.front_999));
              tvAddContacts.setBackgroundColor(v.getResources().getColor(R.color.tran));
              final AlertDialog.Builder normalDialog =
                  new AlertDialog.Builder(v.getContext());
              normalDialog.setMessage("好友请求已发送");
              normalDialog.setPositiveButton("继续添加", null);
              normalDialog.show();
              break;
            case "已添加":
              tvAddContacts.setText("添加");
              tvAddContacts.setTextColor(v.getResources().getColor(R.color.front_white));
              tvAddContacts.setBackgroundColor(v.getResources().getColor(R.color.front_green));
              break;
          }
        }
      });
    }
  }


  class PinnedHolder extends BaseViewHolder {

    TextView city_tip;

    public PinnedHolder(View view) {
      super(view);
      city_tip = findViewById(R.id.city_tip);
    }
  }
}
